package com.scisoft.scan.control.stock;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.scisoft.scan.data.stock.StockInfoBean;
import com.scisoft.scan.data.stock.StockInfoPictureBean;
import com.scisoft.scan.dy.R;
import com.scisoft.scan.util.CommonUtil;
import com.scisoft.scan.ws.WebServiceHelper_Stock;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class StockPictureActivity extends Activity {
    private ImageView iv_Back; //返回按钮
    private TextView tv_submit;//提交按钮

    private ImageView iv_data;
    private byte[] imageData;
    private StockInfoBean stockInfoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_stock_picture);
        try {
            //参数传递
            Intent intent = getIntent();
            //配件
            stockInfoBean = (StockInfoBean) intent.getSerializableExtra("stockInfoBean");
            //图片
            //imageData = intent.getByteArrayExtra("data");
            String mFilePath = intent.getStringExtra("data");
            //初始化控件
            iv_data = findViewById(R.id.iv_data);

//            String sdStatus = Environment.getExternalStorageState();
//            if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
//                Toast.makeText(StockActivity.this, "SD卡不可用", Toast.LENGTH_LONG).show();
//                return;
//            }
            InputStream is = null;
            try {
                // 获取输入流
                is = new FileInputStream(mFilePath);
                // 把流解析成bitmap,此时就得到了清晰的原图
                Bitmap imageBitmap = BitmapFactory.decodeStream(is);
                if (imageBitmap != null) {
                    imageData = CommonUtil.BitmapToBytes(imageBitmap);
                    Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
                    iv_data.setImageBitmap(bitmap);
                } else {
                    Toast.makeText(getApplicationContext(), "读取照片失败：" + mFilePath, Toast.LENGTH_LONG).show();
                }

            } catch (FileNotFoundException e) {
                Toast.makeText(StockPictureActivity.this, "读文件错误：" + e.getMessage(), Toast.LENGTH_LONG).show();
            } finally {
                // 关闭流
                try {
                    if (is != null)
                        is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(StockPictureActivity.this, "关闭文件错误：" + e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }

            TextView tv_title = findViewById(R.id.tv_title);
            tv_title.setText(stockInfoBean.partName);
            //返回
            iv_Back = findViewById(R.id.iv_back);
            iv_Back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    setResult(RESULT_CANCELED, intent);
                    //关闭Activity
                    finish();
                }
            });
            //提交
            tv_submit = findViewById(R.id.tv_submit);
            tv_submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    StockInfoPictureBean stockInfoPictureBean = new StockInfoPictureBean();
                    stockInfoPictureBean.partId = stockInfoBean.partId;
                    stockInfoPictureBean.partCode = stockInfoBean.partCode;
                    stockInfoPictureBean.partNo = stockInfoBean.partNo;
                    stockInfoPictureBean.picture = imageData;
                    CommonUtil.showPress(StockPictureActivity.this);
                    SubmitTask submitTask = new SubmitTask(StockPictureActivity.this, stockInfoPictureBean);
                    submitTask.execute();
                }
            });
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "错误：" + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    //上传配件图片
    class SubmitTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";
        private String data = "";
        private StockInfoPictureBean stockInfoPictureBean;

        SubmitTask(Context context, StockInfoPictureBean stockInfoPictureBean) {
            super();
            mcontext = context;
            this.stockInfoPictureBean = stockInfoPictureBean;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                msg = WebServiceHelper_Stock.STOCK_04(stockInfoPictureBean);
            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                //返回
                Intent intent = new Intent();
                //设置返回数据
                setResult(RESULT_OK, intent);
                finish();
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }
}
